/*
 * Javascript Agile Suite - JasCommonTests
 * http://code.google.com/p/jasproject/
 *
 * Copyright (C) 2009 Luke Schafer
 * Licensed under MIT X11 license:
 * http://www.opensource.org/licenses/mit-license.php
 *
 *
 * Test JasCommon
 * This actually seems a bit silly since JasUnit relies on
 * functions in JasCommon, but it doesn't hurt to be thorough
 */

Jas.Tests = Jas.Tests || {};

Jas.Tests.JasCommon = {
	namespace : "Jas.Tests.JasCommon",

	testStringFormat : function()
	{
		// Test correct behaviour
		Assert.areEqual(
			"The quick brown fox is quick.",
			Jas.stringFormat("The {0} brown {1} is {0}.", "quick", "fox")
		);

		// Test incorrect
		Assert.areNotEqual(
			"The quick brown fox is quick.",
			Jas.stringFormat("The {0} brown {1} is {0}.", "fox", "quick")
		)

		// Test not enough params
		var ex;
		try
		{
			var testString = Jas.stringFormat("{0}{1}{2}", "var1");
		}
		catch (e)
		{
			ex = e;
		}
		Assert.isNotNull(ex);
	},

	testExceptionCreate : function()
	{
		// Exceptions use the format: "[{0}] {1}" where 0 is the type (1st param) and 1 is the message
		var ex = Jas.Exceptions.create("MyEx", "Some Message");
		Assert.areEqual(
			"[MyEx] Some Message",
			ex.toString()
		);
	},

	testIsFunction : function()
	{
		var a = function() {};
		var b = {};
		var c = "";
		var d = new(function construct(){})();

		Assert.isTrue(Jas.isFunction(a));
		Assert.isFalse(Jas.isFunction(b));
		Assert.isFalse(Jas.isFunction(c));
		Assert.isFalse(Jas.isFunction(d));
	},
	
	testExtend : function()
	{
		// Create fakes.
		function foo(){}
		foo.prototype.getName = function() { return this.name; }
		Jas.extend(foo, {setName : function(input) { this.name = input; }});
		var test = new foo();
		
		// Test that it was set up correctly.
		Assert.isNotNull(test);
		Assert.isNotNull(test.setName);

		// Test the function was applied properly.
		test.setName("myName");
		Assert.areEqual("myName", test.getName());
	},

	subFixtures : [{
		namespace : "Jas.Tests.JasCommon.Extensions",

		testFunctionGetInformation : function()
		{
			var func1 = function() {var a = "";};
			var func2 = function exp() {var a = "";};
			var func3 = function exp(arg1, arg2) {var a = "";};
			function func4() {var a = "";};
			function func5(arg) {var a = "";};

			var result;

			result = func1.getInformation();
			Assert.areEqual("", result.name);
			Assert.areEqual(0, result.args.length);
			Assert.areEqual('var a = "";\n', result.body);
			
			result = func2.getInformation();
			Assert.areEqual("exp", result.name);
			Assert.areEqual(0, result.args.length);
			Assert.areEqual('var a = "";\n', result.body);

			result = func3.getInformation();
			Assert.areEqual("exp", result.name);
			Assert.areEqual(2, result.args.length);
			Assert.areEqual("arg1", result.args[0]);
			Assert.areEqual("arg2", result.args[1]);
			Assert.areEqual('var a = "";\n', result.body);
			
			result = func4.getInformation();
			Assert.areEqual("func4", result.name);
			Assert.areEqual(0, result.args.length);
			Assert.areEqual('var a = "";\n', result.body);
			
			result = func5.getInformation();
			Assert.areEqual("func5", result.name);
			Assert.areEqual(1, result.args.length);
			Assert.areEqual('var a = "";\n', result.body);

			// Check against non-functions.
			var notAFunc1, notAFunc2, notAFunc3, notAFunc4;

			try { result = Function.prototype.getInformation({}); }
			catch (e) { notAFunc1 = e; }
			Assert.isNotNull(notAFunc1);

			try { result = Function.prototype.getInformation("test"); }
			catch (e) { notAFunc2 = e; }
			Assert.isNotNull(notAFunc2);

			try { result = Function.prototype.getInformation(null); }
			catch (e) { notAFunc3 = e; }
			Assert.isNotNull(notAFunc3);

			try { result = Function.prototype.getInformation(); }
			catch (e) { notAFunc4 = e; }
			Assert.isNotNull(notAFunc4);
		}
	}]
};